/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-06-01
 * Time: 11:23
 */
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;

    /**
     * 查找
     *
     * @param key
     */
    public TreeNode search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val > key) {
                cur = cur.left;
            } else if (cur.val < key) {
                cur = cur.right;
            } else {
                return cur;
            }
        }
        return null;
    }

    /**
     * 插入
     *
     * @param key
     */
    public void insert(int key) {
        TreeNode newNode = new TreeNode(key);
        if (root == null) {
            root = newNode;
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;

        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                return;
            }
        }
        if (parent.val > key) {
            parent.left = newNode;
        } else {
            parent.right = newNode;
        }
    }

    /**
     * 删除值为key的结点
     *
     * @param key
     */
    public void delete(int key) {
        //先找到待删除的结点
        TreeNode del = root;
        TreeNode parent = null;
        while (del != null) {
            if (key > del.val) {
                parent = del;
                del = del.right;
            } else if (key < del.val) {
                parent = del;
                del = del.left;
            } else {
                //找到了,删除
                remove(parent, del);
                return;
            }
        }
    }

    //实现删除的逻辑
    private void remove(TreeNode parent, TreeNode del) {
        //大情况:

        if (del.left == null) {
            //1.del.left==null,
            //1.1 del是root
            if (del == root) {
                root = del.right;
            } else {
                //del不是root
                if (del == parent.left) {
                    //1.2 del == parent.left
                    parent.left = del.right;
                } else if (del == parent.right) {
                    //1.3 del == parent.right
                    parent.right = del.right;
                }
            }
        } else if (del.right == null) {
            //2.del.right==null,
            if (del == root) {
                // 2.1 del==root
                root = del.left;
            } else {
                if (del == parent.left) {
                    //2.2 del == parent.left
                    parent.left = del.left;
                } else if (del == parent.right) {
                    //2.3 del == parent.right
                    parent.right = del.left;
                }
            }
        } else {
            //3.del.left!=null&&del.right!=null
/*
            //从右子树找最小值
            TreeNode target = del.right;
            TreeNode targetP = del;
            while (target.left != null) {
                targetP = target;
                target = target.left;
            }
            //找到了,替换值
            del.val = target.val;
            //删除
            if (target == targetP.left) {
                targetP.left = target.right;
            } else if (target == targetP.right) {
                targetP.right = target.right;
            }*/

            //从左子树找最大值
            TreeNode target = del.left;
            TreeNode targetP = del;
            while (target.right != null) {
                targetP = target;
                target = target.right;
            }
            if (target == targetP.right) {
                targetP.right = target.left;
            } else if (target == targetP.left) {
                targetP.left = target.left;
            }
        }


    }


}
